//
//  MineViewController.m
//  VTravel
//
//  Created by lanouhn on 16/8/1.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "MineViewController.h"

#import "LoginViewController.h"
#import "CollectionViewController.h"

#import "HeaderView.h"
#import "FooterView.h"
#import "UIImageView+WebCache.h"

@interface MineViewController ()<UITableViewDelegate, UITableViewDataSource>
/* 表视图 **/
@property (strong, nonatomic) IBOutlet UITableView *tableView;
/* 数据源数组 **/
@property (nonatomic, strong) NSArray *dataArray;
/* 表头视图**/
@property (nonatomic, strong) HeaderView *headerView;
/* 表尾视图 **/
@property (nonatomic, strong) FooterView *footerView;
/** 清理缓存的Label*/
@property (nonatomic, strong) UILabel *label1;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataArray = @[@"我的收藏", @"常用游客资料", @"我的旅历", @"个人资料", @"修改密码", @"清除缓存", @"帮助中心", @"关于微旅"];
    
    /* 配置表头表尾 **/
    self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];
    
    self.headerView.headerImageView.hidden = YES;
    self.headerView.nameLabel.hidden = YES;
    
    [self.headerView.button addTarget:self action:@selector(LoginView) forControlEvents:UIControlEventTouchUpInside];
    
    self.footerView = [[FooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    [self.footerView.button addTarget:self action:@selector(LogOutView) forControlEvents:UIControlEventTouchUpInside];
    
    /* 配置tableView **/
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;
    [self.view addSubview:self.tableView];
}
#pragma mark - 表头登录/注册按钮点击事件
- (void)LoginView{
    LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    loginVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:loginVC animated:YES];
}
#pragma mark - 表尾退出登录按钮点击事件
- (void)LogOutView{
    NSLog(@"退出登录");
}
#pragma mark - tableView代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 5;
            break;
        case 1:
            return 3;
            break;
        default:
            break;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.textLabel.text = self.dataArray[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:self.dataArray[indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 150, 0, 100, 42)];
            self.label1.text = [NSString stringWithFormat:@"%.2fM",[self clearCache]];
            self.label1.font = [UIFont systemFontOfSize:12];
            self.label1.textAlignment = NSTextAlignmentRight;
            [cell addSubview:self.label1];
        }
        cell.textLabel.text = self.dataArray[indexPath.row + 5];
        cell.imageView.image = [UIImage imageNamed:self.dataArray[indexPath.row + 5]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
}
/* 区头高度 **/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
/* 点击cell的方法 **/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        kMyLog(@"%@", self.dataArray[indexPath.row]);
        if (indexPath.row == 0) {
            CollectionViewController *collectionVC = [[CollectionViewController alloc] init];
            [self.navigationController pushViewController:collectionVC animated:YES];
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            // 创建提示框
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"确定要清除%.2fM缓存吗?", [self clearCache]] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self removeCache];
                self.label1.text = nil;
                [self.tableView reloadData];
            }];
            
            UIAlertAction *acceptAction = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            // 将事件给控制器
            [alertController addAction:rejectAction];
            [alertController addAction:acceptAction];
            // 弹出提示框
            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
        }
        kMyLog(@"%@", self.dataArray[indexPath.row + 5]);
    }
}

#pragma mark - 清除缓存
// 计算单个文件的大小
- (float)fileSizeAtPath:(NSString *)path{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        return size / 1024. / 1024.0;
    }
    return 0;
}
// 找到缓存的文件夹
- (float)clearCache{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager *manager = [NSFileManager defaultManager];
    float floderSize;
    if ([manager fileExistsAtPath:path]) {
        // 拿到有文件的数组
        NSArray *childerFile = [manager subpathsAtPath:path];
        // 遍历数组,拿到每个文件的名字,如果不想清除的文件,就在这里判断
        for (NSString *fileName in childerFile) {
            // 将路径拼接在一起
            NSString *filePath = [path stringByAppendingPathComponent:fileName];
            floderSize += [self fileSizeAtPath:filePath];
        }
    }
    return floderSize;
}
// 清除缓存
- (void)removeCache{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        // 拿到有文件的数组
        NSArray *childerFile = [manager subpathsAtPath:path];
        // 遍历数组,拿到每个文件的名字,如果不想清除的文件,就在这里判断
        for (NSString *fileName in childerFile) {
            // 将路径拼接在一起
            NSString *filePath = [path stringByAppendingPathComponent:fileName];
            [manager removeItemAtPath:filePath error:nil];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
